package cn.yuan.xiaoyu.commonmodule.manager;


import cn.yuan.xiaoyu.caipumodule.bean.CaiPuListBean;
import cn.yuan.xiaoyu.caipumodule.bean.CaiPuTagListBean;
import cn.yuan.xiaoyu.chatmodule.bean.SendMessageBean;
import cn.yuan.xiaoyu.commonmodule.config.Constant;
import cn.yuan.xiaoyu.commonmodule.config.ConstantUrl;
import cn.yuan.xiaoyu.commonmodule.net.OkHttpUtil;
import cn.yuan.xiaoyu.commonmodule.net.RequestPacket;
import cn.yuan.xiaoyu.commonmodule.net.ResponseListener;
import cn.yuan.xiaoyu.jokemodule.bean.JokeListBean;
import cn.yuan.xiaoyu.newsmodule.bean.TouTiaolistBean;


/**
 * Created by yukuoyuan on 2016/6/12.
 * 这是一个所有网络请求的管理工具类
 */
public class HttpManager {
    private static HttpManager httpManager;

    private HttpManager() {
    }

    public static HttpManager getInstance() {
        if (httpManager == null) {
            synchronized (HttpManager.class) {
                httpManager = new HttpManager();
            }
        }
        return httpManager;
    }

    /**
     * 这是一个发送消息的方法
     *
     * @param uid              用户的手机号
     * @param msg              发送的消息
     * @param responseListener 回调监听
     */
    public void sendMsg(String uid, String msg, ResponseListener<SendMessageBean> responseListener) {
        RequestPacket requestPacket = new RequestPacket();
        requestPacket.url = ConstantUrl.getBaseUrl();
        requestPacket.addArgument("key", Constant.AppKey);
        requestPacket.addArgument("info", msg);
        requestPacket.addArgument("userid", uid);
        OkHttpUtil.Request(RequestPacket.GET, requestPacket, responseListener);

    }

    /**
     * 这是一个获取笑话列表的方法
     *
     * @param page             第几页数据
     * @param pagesize         每页多少条数据
     * @param responseListener 回调监听
     */
    public void getJokeList(int page, int pagesize, ResponseListener<JokeListBean> responseListener) {
        RequestPacket requestPacket = new RequestPacket();
        requestPacket.url = ConstantUrl.getJokeUrl;
        requestPacket.addArgument("key", Constant.JokeAppkey);
        requestPacket.addArgument("page", page);
        requestPacket.addArgument("pagesize", pagesize);
        OkHttpUtil.Request(RequestPacket.GET, requestPacket, responseListener);

    }

    /**
     * 这是一个获取所有的菜谱大全 分类列表的方法
     *
     * @param responseListener 回调监听
     */
    public void getCaiPuTagList(ResponseListener<CaiPuTagListBean> responseListener) {
        RequestPacket requestPacket = new RequestPacket();
        requestPacket.url = ConstantUrl.getCaiPuUrl;
        requestPacket.addArgument("key", Constant.CaipuAppkey);
        OkHttpUtil.Request(RequestPacket.GET, requestPacket, responseListener);

    }

    /**
     * 这是一个获取菜谱列表的方法
     *
     * @param page             页面的角标
     * @param tagid            标签id
     * @param responseListener 回调监听
     */
    public void getCaiPuList(int page, int tagid, ResponseListener<CaiPuListBean> responseListener) {
        RequestPacket requestPacket = new RequestPacket();
        requestPacket.url = ConstantUrl.getCaiPuListUrl;
        requestPacket.addArgument("key", Constant.CaipuAppkey);
        requestPacket.addArgument("menu", tagid);
        requestPacket.addArgument("pn", page);
        requestPacket.addArgument("rn", 10);
        OkHttpUtil.Request(RequestPacket.GET, requestPacket, responseListener);

    }

    /**
     * 这是一个获取今日头条新闻数据的方法
     *
     * @param query            新闻类型
     * @param responseListener 回调监听
     */
    public void getNewsList(String query, ResponseListener<TouTiaolistBean> responseListener) {
        RequestPacket requestPacket = new RequestPacket();
        requestPacket.url = ConstantUrl.gettoutiaoApi;
        requestPacket.addArgument("utm_source", "toutiao");
        requestPacket.addArgument("widen", 1);
        requestPacket.addArgument("max_behot_time_tmp", 0);
        requestPacket.addArgument("as", "A1C528E25E76FB8");
        requestPacket.addArgument("cp", "582EC64FEBD84E1");
        requestPacket.addArgument("max_behot_time", 0);
        requestPacket.addArgument("category", query);
        requestPacket.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.108 Safari/537.36 2345Explorer/8.0.0.13547");
        requestPacket.addHeader("Cache-Controlt", "max-age=0");
        requestPacket.addHeader("Upgrade-Insecure-Requests", "1");
        requestPacket.addHeader("X-Requested-With", "XMLHttpRequest");
        requestPacket.addHeader("Cookie", "uuid=\"w:f2e0e469165542f8a3960f67cb354026\"; __tasessionId=4p6q77g6q1479458262778; csrftoken=7de2dd812d513441f85cf8272f015ce5; tt_webid=36385357187");
        OkHttpUtil.Request(RequestPacket.GET, requestPacket, responseListener);
    }
}
